﻿using System;
using System.Collections.Generic;
using System.Linq;
using Mvc3App.Dominio;

namespace Mvc3App.Repositorio
{
    //public class SocioMemoryRepository : IRepository<Socio>
    public class SocioMemoryRepository
    {
        static IList<Socio> _repository;
        static int _currentId = 0;

        public SocioMemoryRepository()
        {
            if (_repository == null)
            {
                _repository = new List<Socio>();
                GenerarDatos();
            }
        }

        private void GenerarDatos()
        {
            for (int i = 1; i <= 30; i++)
            {
                var socio = new Socio { Id = GetNewId(), Apellido = "Apellido_" + i, Nombre = "Nombre " + i, Documento = i.ToString(), FechaIngreso = DateTime.Today };
                _repository.Add(socio);
            }
        }

        private int GetNewId()
        {
            _currentId = _currentId + 1;
            return _currentId;
        }

        #region Implementation of IRepository<Socio>

        public Socio GetOne(object id)
        {
            return _repository.SingleOrDefault(x => x.Id == (int)id);
        }

        public long Count()
        {
            return _repository.Count();
        }

        public void Add(Socio entity)
        {
            entity.Id = GetNewId();
            _repository.Add(entity);
        }

        public void Add(IEnumerable<Socio> entities)
        {
            foreach (var entity in entities)
            {
                Add(entity);
            }
        }

        public void Update(Socio entity)
        {
            var old = GetOne(entity.Id);
            _repository.Remove(old);
            _repository.Add(entity);
        }

        public void Delete(object id)
        {
            var socio = GetOne(id);
            _repository.Remove(socio);
        }

        public IEnumerable<Socio> GetSlice(int slice, int quantity, Func<Socio, IComparable> order, bool ascending)
        {
            IEnumerable<Socio> result = ascending ? _repository.OrderBy(order) : _repository.OrderByDescending(order);
            return result.Skip((slice - 1) * quantity).Take(quantity);
        }

        #endregion

        public IEnumerable<Socio> GetAll(string orderByField, bool ascending)
        {
            return _repository.OrderBy(x => x.Apellido);
        }

        public IEnumerable<Socio> GetAll()
        {
            return _repository;
        }

        public IEnumerable<Socio> GetByFiltro(string name, string lastname, string dni)
        {
            return _repository.Where(x => (string.IsNullOrEmpty(name) || x.Nombre.Contains(name))  &&
                (string.IsNullOrEmpty(lastname) || x.Apellido.Contains(lastname)) &&
                (string.IsNullOrEmpty(dni) || x.Documento.Contains(dni)));
        }

        public IEnumerable<Socio> GetSliceByFilter(int slice, int quantity, Func<Socio, IComparable> order, bool ascending, string name, string lastname, string dni)
        {
            var resultFilters = GetByFiltro(name, lastname, dni);
            IEnumerable<Socio> result = ascending ? resultFilters.OrderBy(order) : resultFilters.OrderByDescending(order);
            return result.Skip((slice - 1) * quantity).Take(quantity);
        }
    }
}